package com.shanghaionstar.mro.util;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.sqlite.entities.RequestEventReportEntity;
import com.onstar.cn.framework.network.http.ResponseHandler;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.DealerBrandType;
import com.shanghaionstar.mro.MrOActivity;
import com.shanghaionstar.mro.OnClickListene;
import com.shanghaionstar.mro.view.FenbeiWave;
import com.shanghaionstar.mro.view.MrOBrand;
import com.shanghaionstar.mro.view.MrOFootLayout;
import com.shanghaionstar.mro.view.Round;
import com.shanghaionstar.pin.PinDialogFragment;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.OperateReportUtil;
import com.shanghaionstar.utils.RequestUtil;
import com.vcyber.sdk.foro.RecDialogForOListener;

import java.util.ArrayList;

public class MrOAnimationUtil {

    private MrOActivity mroActivity;
    private FenbeiWave fenbeiWave;
    private ArrayList<Float> volumes;
    private int volumesCount;
    private View animationView;
    private View bodyView;
    private View footMro;
    private MrOFootLayout footView;
    private ImageView iv_big_point;
    private TextView mro_input_content;
    private TextView mro_reply_content;
    private Round round;
    private MrOBrand mrOBrand;
    private VoiceUtil voiceUtil;
    private MyRecDialogForOListener myRecDialogForOListener;
    private FrameLayout mro_result_fragment_container;
    private MrOPlayEntity mrOPlayEntity;

    private TextView temp_mro_voice_text;

    public MrOAnimationUtil(View animationView, MrOActivity mroActivity, View bodyView, MrOFootLayout footView) {
        this.animationView = animationView;
        this.bodyView = bodyView;
        this.mroActivity = mroActivity;
        this.footView = footView;
        this.footMro = footView.findViewById(R.id.mro_bottom_xiaoo);
        initializeElements();
    }

    private void initializeElements() {
        if (mrOBrand == null)
            iv_big_point = (ImageView) animationView.findViewById(R.id.iv_big_point);
        if (round == null)
            round = (Round) animationView.findViewById(R.id.round);
        if (mro_input_content == null)
            mro_input_content = (TextView) animationView.findViewById(R.id.mro_input_content);
        if (mro_reply_content == null)
            mro_reply_content = (TextView) animationView.findViewById(R.id.mro_reply_content);
        if (mro_reply_content == null)
            mro_reply_content.setMovementMethod(ScrollingMovementMethod.getInstance());
        if (mro_result_fragment_container == null)
            mro_result_fragment_container = (FrameLayout) animationView.findViewById(R.id.mro_result_fragment_container);
        if (volumes == null)
            volumes = new ArrayList<Float>();
        if (mrOBrand == null)
            mrOBrand = (MrOBrand) (animationView.findViewById(R.id.layout_mro_brand));
        if (fenbeiWave == null) {
            fenbeiWave = (FenbeiWave) animationView.findViewById(R.id.fenbei);
        }
        if (temp_mro_voice_text == null)
            temp_mro_voice_text = (TextView) animationView.findViewById(R.id.temp_mro_voice_text);
        round.setOnClickListene(new OnClickListene() {
            @Override
            public void onCallBackIsShow(Boolean isGoOn) {
                if (isGoOn && animationView.getVisibility() == View.VISIBLE) {
                    round.setVisibility(View.INVISIBLE);
                    mrOBrand.setVisibility(View.GONE);
                    startAnimation();
                }
            }
        });
        myRecDialogForOListener = new MyRecDialogForOListener();
    }

    // 每次动画再次开始要初始化
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void initView() {
        checkFigureConfig();
        if (mrOBrand != null)
            mrOBrand.setVisibility(View.VISIBLE);
        volumes.clear();
        mro_input_content.setVisibility(View.GONE);
        mro_input_content.setAlpha(1);
        mro_input_content.setTranslationY(0);
        mro_reply_content.setVisibility(View.GONE);
        mro_result_fragment_container.setVisibility(View.GONE);
        mro_result_fragment_container.removeAllViews();
    }

    public void checkFigureConfig() {
        if (mrOBrand == null || footView == null)
            return;
        String currentFigure = PreferencesUtls.getInstance(mroActivity).getString(FinalConstants.MroConstants.MRO_FIGURE_KEY);
        if ("".equals(currentFigure) || FinalConstants.MroConstants.MRO_FIGURE_DEFAULT.equals(currentFigure)) {
            mrOBrand.changeBrand(DealerBrandType.DEFAULT);
            footView.changeBrand(DealerBrandType.DEFAULT);
        } else if (FinalConstants.MroConstants.MRO_FIGURE_BRAND.equals(currentFigure)) {
            DealerBrandType myBrand = DealerBrandType.getDealerBrandTypeByDesc(ApplicationUtil.getInstance().getCarInfo());
            mrOBrand.changeBrand(myBrand);
            footView.changeBrand(myBrand);
        } else if (FinalConstants.MroConstants.MRO_FIGURE_WTK.equals(currentFigure)) {
            mrOBrand.changeBrand(DealerBrandType.WTK);
            footView.changeBrand(DealerBrandType.WTK);
        }
    }

    private void focus() {
        if (round != null)
            round.setIsReduce(true);
    }

    public void focus(MrOPlayEntity mrOPlayEntity) {
        this.mrOPlayEntity = mrOPlayEntity;
        focus();
    }

    public void circle() {
        if (fenbeiWave != null)
            fenbeiWave.setVisibility(View.INVISIBLE);
        if (round != null) {
            round.initView();
            round.setIsBegin(true);
            round.setVisibility(View.VISIBLE);
        }
        if (mrOBrand != null) {
            mrOBrand.loading();
        }
    }

    public void quickShowResult(String replyMessage) {
        stopRecord();
        initView();
        mrOBrand.setVisibility(View.GONE);
        showReplyMessage(replyMessage, null);
    }

//    public void startListen() {
//        startListen(false);
//    }

    public void startListen(boolean isFeedback) {
        if (mrOBrand != null) {
            mrOBrand.listen();
            startTTSRecord(isFeedback);
        }
    }

    private void startTTSRecord(final boolean isFeedback) {
        if (fenbeiWave != null)
            fenbeiWave.setVisibility(View.VISIBLE);
        try {
            myRecDialogForOListener.syncHandler(gpsHandler);
            volumes.clear();
            volumesCount = 0;
            voiceUtil = new VoiceUtil();
            voiceUtil.updateResource(mroActivity, myRecDialogForOListener);
//            voiceUtil.stopSession();
            voiceUtil.stopTts();
            if (!isFeedback)
                voiceUtil.poi();
            else {
                voiceUtil.feedback();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 光点自由落体动画
     */
    private Animation animation1;
    private Animation animation2;

    private void startAnimation() {
        animation1 = AnimationUtils.loadAnimation(mroActivity, R.anim.mro_push_in);// 底部小o出现
        animation2 = AnimationUtils.loadAnimation(mroActivity, R.anim.mro_scale);// 由小变大
        animation2.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                // 防止动画未做完，用户按back键销毁对象，空指针错误
                if (footMro != null) {
                    footMro.setVisibility(View.VISIBLE);
                    footMro.startAnimation(animation1);
                }
            }
        });
        iv_big_point.startAnimation(animation2);
        animation1.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation arg0) {
                iv_big_point.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // 防止动画未做完，用户按back键销毁对象，空指针错误
                if (bodyView != null && animationView != null && footMro != null && animationView.getVisibility() == View.VISIBLE) {
//                    bodyView.setVisibility(View.VISIBLE);
//                    animationView.setVisibility(View.GONE);
                    mroActivity.showBottomMrO(true);
                    showResults();
                }
            }
        });

    }

    private void showResults() {
        if (mrOPlayEntity != null) {
            if (mrOPlayEntity.playFragment != null) {
                showResultFragment(mrOPlayEntity.playFragment, mrOPlayEntity.playMessage == null ? mrOPlayEntity.replyMessage : mrOPlayEntity.playMessage);
            } else if (mrOPlayEntity.replyMessage != null) {
                if (mrOPlayEntity.needPlay) {
                    if (mrOPlayEntity.playMessage != null) {
                        showReplyMessage(mrOPlayEntity.replyMessage, mrOPlayEntity.playMessage);
                    } else {
                        showReplyMessage(mrOPlayEntity.replyMessage, mrOPlayEntity.replyMessage);
                    }
                } else if (mrOPlayEntity.replyMessage.equalsIgnoreCase("H5")) {
                    final String msg = mrOPlayEntity.playMessage;
                    showMsgDia(msg);
                } else if (mrOPlayEntity.replyMessage.equalsIgnoreCase("position")) {
                    mroActivity.jumpToNav(mroActivity.getString(R.string.find_vehicle), mrOPlayEntity.playMessage);
                } else {
                    showReplyMessage(mrOPlayEntity.replyMessage, null);
                }
            }
            mrOPlayEntity = null;
        }
    }

    private void showMsgDia(final String msg) {
        int stringId = -1;
        String value = MrOJsonUtil.getValue(msg);
        if (value.equalsIgnoreCase("driving")) {
            stringId = R.string.mro_dia_drivering;
        } else if (value.equalsIgnoreCase("ordering")) {
            stringId = R.string.mro_dia_ordering;
        } else if (value.equalsIgnoreCase("golf")) {
            stringId = R.string.mro_dia_golf;
        }
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mroActivity);
        alertDialog.setMessage(stringId);
        alertDialog.setPositiveButton(R.string.mro_go_see, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mroActivity.jumpToWebview(msg);
            }
        });
        alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        alertDialog.show();

//        AlertDialogUtil.showAlertDialog(mroActivity, stringId, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                mroActivity.jumpToWebview(msg);
//            }
//        }, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
////                            showReplyMessage(null, null);
//            }
//        });
    }

    private void showMyWords(String words) {
        mro_input_content.setVisibility(View.VISIBLE);
        mro_input_content.setText(words);
        if (Build.VERSION.SDK_INT >= 11) {
            mro_input_content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onGlobalLayout() {
                    ObjectAnimator.ofFloat(mro_input_content, "translationX", mro_input_content.getWidth() +
                            ((RelativeLayout.LayoutParams) mro_input_content.getLayoutParams()).rightMargin, 0)
                            .setDuration(500)
                            .start();
                    if (Build.VERSION.SDK_INT >= 16)
                        mro_input_content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    else
                        mro_input_content.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
        }
    }

    private void showReplyMessage(String replyMessage, String playMessage) {
        if (playMessage != null && voiceUtil != null)
            voiceUtil.playFreeText(playMessage);
        mro_reply_content.setVisibility(View.VISIBLE);
        mro_reply_content.setText(replyMessage);
        if (Build.VERSION.SDK_INT >= 11) {
            mro_reply_content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onGlobalLayout() {
                    AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(mroActivity, R.animator.mro_replymsg_show);
                    animatorSet.setTarget(mro_reply_content);
                    animatorSet.start();
                    if (Build.VERSION.SDK_INT >= 16)
                        mro_reply_content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    else
                        mro_reply_content.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
        }
    }

    private void showResultFragment(Fragment resultFragment, String playMessage) {
        if (playMessage != null && voiceUtil != null) {
            voiceUtil.playFreeText(playMessage);
        }
        if (resultFragment instanceof PinDialogFragment) {
//            PinDialogFragment pinDialogFragment = (PinDialogFragment) resultFragment;
//            pinDialogFragment.show(mroActivity.fragmentManager, PinDialogFragment.class.getSimpleName());
        } else {
            mroActivity.changeResultFragment(mroActivity.fragmentManager, resultFragment, resultFragment.getClass(), false);
        }
        mro_result_fragment_container.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= 11) {
            mro_result_fragment_container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onGlobalLayout() {
                    AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(mroActivity, R.animator.mro_replymsg_show);
                    animatorSet.setTarget(mro_result_fragment_container);
                    animatorSet.start();
                    if (mro_input_content.getVisibility() == View.VISIBLE) {
                        Animator alpha = ObjectAnimator.ofFloat(mro_input_content, "alpha", 1, 0);
                        Animator translationY = ObjectAnimator.ofFloat(mro_input_content, "translationY", 0, -mro_input_content.getHeight() -
                                ((RelativeLayout.LayoutParams) mro_input_content.getLayoutParams()).topMargin);
                        AnimatorSet animatorSet1 = new AnimatorSet();
                        animatorSet1.setDuration(500);
                        animatorSet1.playTogether(alpha, translationY);
                        animatorSet1.start();
                    }
                    if (Build.VERSION.SDK_INT >= 16)
                        mro_result_fragment_container.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    else
                        mro_result_fragment_container.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
        } else {
            mro_input_content.setVisibility(View.GONE);
        }
    }

    private void showBottomMrO() {
        try {
            fenbeiWave.setVisibility(View.INVISIBLE);
            round.setVisibility(View.INVISIBLE);
            mrOBrand.setVisibility(View.GONE);
            footMro.setVisibility(View.VISIBLE);
            mroActivity.showBottomMrO(true);
        } catch (Exception e) {
        }
    }

    private void stopRecordWithMessage(String message) {
        stopRecord();
        mroActivity.animationFinish();
        showBottomMrO();
        showReplyMessage(message, null);
    }

    public void stopRecord() {
        if (fenbeiWave != null)
            fenbeiWave.setVisibility(View.INVISIBLE);
        if (voiceUtil != null)
            voiceUtil.stopSession();
        if (myRecDialogForOListener != null)
            myRecDialogForOListener.syncHandler(null);
        if (iv_big_point != null && iv_big_point.getAnimation() != null) {
            iv_big_point.getAnimation().cancel();
            iv_big_point.getAnimation().setAnimationListener(null);
        }
        if (footMro != null && footMro.getAnimation() != null) {
            footMro.getAnimation().cancel();
            footMro.getAnimation().setAnimationListener(null);
        }
        if (round != null) {
            round.initView();
            round.stop();
        }
    }

    private Handler gpsHandler = new Handler() {
        @Override
        public void handleMessage(Message paramMessage) {
            super.handleMessage(paramMessage);
            LogManager.getInstance().d("the valums is--->" + paramMessage.obj);
            switch (paramMessage.what) {
                case FinalConstants.MroConstants.ONTHERESULT: {
                    String msg = paramMessage.obj.toString();
                    LogManager.getInstance().e("onTheResult msg=" + msg);
//                    temp_mro_voice_text.setText(msg);
                    String oprType = MrOJsonUtil.getOperationType(msg);
                    String status = MrOJsonUtil.getStatus(msg);
                    boolean isSuccess = "success".equalsIgnoreCase(status);
                    LogManager.getInstance().d("the status is--->" + status);
                    if ("Initialize".equalsIgnoreCase(oprType)) {
                        if (isSuccess && mroActivity != null) {
                            mroActivity.setupEventEntity();
                        }
                        if ("beginsilencetimeout".equals(status)) {
                            stopRecordWithMessage(mroActivity.getString(R.string.MRO_002));
                        } else if (!isSuccess) {
                            stopRecordWithMessage(mroActivity.getString(R.string.MRO_003));
                        }
                    } else if ("error".equalsIgnoreCase(status)) {
                        stopRecordWithMessage(mroActivity.getString(R.string.MRO_004));
                    } else if ("fail".equalsIgnoreCase(status)) {
                        stopRecordWithMessage(mroActivity.getString(R.string.MRO_005));
                    } else if ("Feedback".equalsIgnoreCase(oprType)) {
                        showBottomMrO();
                        showMyWords(MrOJsonUtil.getContent(msg));
                        String answer = MrOJsonUtil.getAnswer(msg);
                        if(!TextUtils.isEmpty(answer))
                            showReplyMessage(MrOJsonUtil.getAnswer(msg),null);
//                        showReplyMessage(mroActivity.getString(R.string.UC011_MSG004), null);
                        final String _type = MrOJsonUtil.getValue(msg);
                        if (!TextUtils.isEmpty(_type)) {
                            mroActivity.putReport(_type);
                        }
                        RequestUtil.getInstance().getFeedback(MrOJsonUtil.getContent(msg), new ResponseHandler() {
                            @Override
                            public void onSuccess(String tag, String json) {
                                sendMroReport(_type, OperateReportUtil.CONTENT_SUCCESS, mroActivity);
                            }

                            @Override
                            public void onError(String tag, HttpException e) {
                                sendMroReport(_type, OperateReportUtil.CONTENT_FAIL, mroActivity);
                            }
                        }, mroActivity, true);

                    } else {
                        circle();
                        showMyWords(MrOJsonUtil.getContent(msg));
                        mroActivity.processListenResult(msg);
                    }
                    break;
                }
                case FinalConstants.MroConstants.ONERROR: {
                    stopRecordWithMessage(mroActivity.getString(R.string.MRO_004));
//                    double volume = (Double) paramMessage.obj;
//                    volumes.add((float) volume);
//                    if (fenbeiWave != null) {
//                        fenbeiWave.setVolumes(volumes);
//                        fenbeiWave.postInvalidate();
//                    }
                    break;
                }
                case FinalConstants.MroConstants.ONVOLUME: {
                    double volume = (Double) paramMessage.obj;
                    volumes.add((float) volume);
                    if (fenbeiWave != null) {
                        fenbeiWave.setVolumes(volumes);
                        fenbeiWave.postInvalidate();
                    }
                    break;
                }
                case FinalConstants.MroConstants.ONPLAYVOLUME: {
                    double volume = (Double) paramMessage.obj;
                    volumes.add((float) volume);
                    if (fenbeiWave != null) {
                        fenbeiWave.setVolumes(volumes);
                        fenbeiWave.postInvalidate();
                    }
                    break;
                }

                case FinalConstants.MroConstants.ONENDPLAY: {
                    volumes.clear();
                    if (fenbeiWave != null) {
                        fenbeiWave.postInvalidate();
                        fenbeiWave.setVolumes(volumes);
                    }
                    break;
                }
//                case 2222:
//                    Toast.makeText(mroActivity.getApplicationContext(), "请说话！", Toast.LENGTH_LONG).show();
//                    break;
//                case 30000:
//                    Toast.makeText(mroActivity.getApplicationContext(), "请检查录音权限是否开启！", Toast.LENGTH_LONG).show();
//                    break;
                default:
                    break;
            }
        }
    };

    public void release() {
        if (fenbeiWave != null) {
            ((ViewGroup) fenbeiWave.getParent()).removeAllViews();
            fenbeiWave = null;
        }
        if (footView != null) {
            ((ViewGroup) footView.getParent()).removeAllViews();
            footView = null;
        }
        if (round != null) {
            round.stop();
            round.setOnClickListene(null);
//            ((ViewGroup) round.getParent()).removeAllViews();
            round = null;
        }
        mrOBrand = null;
        if (iv_big_point != null)
            iv_big_point.clearAnimation();
        iv_big_point = null;
        animationView = null;
        bodyView = null;
        if (footMro != null)
            footMro.clearAnimation();
        footMro = null;
        mrOBrand = null;
        animation1 = null;
        animation2 = null;
        if (voiceUtil != null)
            voiceUtil.recycleVD();
        voiceUtil = null;
        mroActivity = null;
        if (myRecDialogForOListener != null)
            myRecDialogForOListener.release();
        myRecDialogForOListener = null;
        mrOPlayEntity = null;
        System.gc();
    }

    private void sendMroReport(String _oprType, String contentSuccess, MrOActivity mroActivity) {
        RequestEventReportEntity mEventReport = mroActivity.getReport(_oprType);
        if (mEventReport != null) {
            mEventReport.setEndTimestamp(OperateReportUtil.getDateFromPattern(OperateReportUtil.pattern));
            mEventReport.setOperation(_oprType);
            mEventReport.setContent(contentSuccess);
            OperateReportUtil.operateEventReportFroMro(mEventReport, mroActivity);
            mroActivity.removeReport(_oprType);
        }
    }

    private class MyRecDialogForOListener extends RecDialogForOListener {
        private Handler myHandler;

        public void syncHandler(final Handler handler) {
//            try {
//                Thread.currentThread().sleep(3000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            if (myHandler == null)
                this.myHandler = handler;
        }

        @Override
        public void onTheResult(String arg0) {
            synchronized (myHandler) {
                if (myHandler == null)
                    return;
                Message msg = myHandler.obtainMessage();
                msg.what = FinalConstants.MroConstants.ONTHERESULT;
                msg.obj = arg0;
                myHandler.sendMessage(msg);
            }
        }


        @Override
        public void onVolume(final double arg0) {
            synchronized (myHandler) {
                if (myHandler == null)
                    return;
                Message msg = myHandler.obtainMessage();
                msg.what = FinalConstants.MroConstants.ONVOLUME;
                msg.obj = arg0;
                myHandler.sendMessage(msg);
            }
        }

        @Override
        public void onPlayVolume(final double arg0) {
            synchronized (myHandler) {
                if (myHandler == null)
                    return;
                Message msg = myHandler.obtainMessage();
                msg.what = FinalConstants.MroConstants.ONPLAYVOLUME;
                msg.obj = arg0;
                myHandler.sendMessage(msg);
            }
        }

        //sdk内部出错会返回错误码
        @Override
        public void onError(int speecherror) {
            synchronized (myHandler) {
                if (myHandler == null)
                    return;
                Message msg = myHandler.obtainMessage();
                msg.what = FinalConstants.MroConstants.ONERROR;
                msg.obj = speecherror;
                myHandler.sendMessage(msg);
            }
        }

        @Override
        public void onEndPlay() {
            synchronized (myHandler) {
                if (myHandler == null)
                    return;
                super.onEndPlay();
                Message msg = myHandler.obtainMessage();
                msg.what = FinalConstants.MroConstants.ONENDPLAY;
                myHandler.sendMessage(msg);
            }
        }

        public void release() {
            if (myHandler != null)
                myHandler.removeCallbacksAndMessages(null);
            syncHandler(null);
        }

    }
}
